<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <meta name="generator" content="VuePress 2.0.0-beta.38">
    <style>
      :root {
        --c-bg: #fff;
      }
      html.dark {
        --c-bg: #22272e;
      }
      html, body {
        background-color: var(--c-bg);
      }
    </style>
    <script>
      const userMode = localStorage.getItem('vuepress-color-scheme');
			const systemDarkMode = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
			if (userMode === 'dark' || (userMode !== 'light' && systemDarkMode)) {
				document.documentElement.classList.toggle('dark', true);
			}
    </script>
    <link rel="icon" href="/imgs/favicon.ico"><title>基本使用 | Webpack</title><meta name="description" content="技术型高端人才培训计划系列课程">
    <link rel="modulepreload" href="/webpack5-doc/assets/app.69823687.js"><link rel="modulepreload" href="/webpack5-doc/assets/base.html.678b0dd3.js"><link rel="modulepreload" href="/webpack5-doc/assets/base.html.3eb57591.js">
    <link rel="stylesheet" href="/webpack5-doc/assets/style.d7931ed9.css">
  </head>
  <body>
    <div id="app"><!--[--><div class="theme-container"><!--[--><header class="navbar"><div class="toggle-sidebar-button" title="toggle sidebar" aria-expanded="false" role="button" tabindex="0"><div class="icon" aria-hidden="true"><span></span><span></span><span></span></div></div><span><a href="/webpack5-doc/" class=""><img class="logo" src="/webpack5-doc/imgs/favicon.ico" alt="Webpack"><span class="site-name can-hide">Webpack</span></a></span><div class="navbar-items-wrapper" style=""><!--[--><!--]--><nav class="navbar-items can-hide"><!--[--><div class="navbar-item"><a href="/webpack5-doc/base/" class="router-link-active" aria-label="基础"><!--[--><!--]--> 基础 <!--[--><!--]--></a></div><div class="navbar-item"><a href="/webpack5-doc/progress/" class="" aria-label="进阶"><!--[--><!--]--> 进阶 <!--[--><!--]--></a></div><div class="navbar-item"><a href="/webpack5-doc/senior/" class="" aria-label="高级"><!--[--><!--]--> 高级 <!--[--><!--]--></a></div><div class="navbar-item"><a href="/webpack5-doc/project/" class="" aria-label="项目"><!--[--><!--]--> 项目 <!--[--><!--]--></a></div><div class="navbar-item"><a href="/webpack5-doc/origin/" class="" aria-label="原理"><!--[--><!--]--> 原理 <!--[--><!--]--></a></div><!--]--></nav><!--[--><!--]--><button class="toggle-dark-button" title="toggle dark mode"><svg style="" class="icon" focusable="false" viewBox="0 0 32 32"><path d="M16 12.005a4 4 0 1 1-4 4a4.005 4.005 0 0 1 4-4m0-2a6 6 0 1 0 6 6a6 6 0 0 0-6-6z" fill="currentColor"></path><path d="M5.394 6.813l1.414-1.415l3.506 3.506L8.9 10.318z" fill="currentColor"></path><path d="M2 15.005h5v2H2z" fill="currentColor"></path><path d="M5.394 25.197L8.9 21.691l1.414 1.415l-3.506 3.505z" fill="currentColor"></path><path d="M15 25.005h2v5h-2z" fill="currentColor"></path><path d="M21.687 23.106l1.414-1.415l3.506 3.506l-1.414 1.414z" fill="currentColor"></path><path d="M25 15.005h5v2h-5z" fill="currentColor"></path><path d="M21.687 8.904l3.506-3.506l1.414 1.415l-3.506 3.505z" fill="currentColor"></path><path d="M15 2.005h2v5h-2z" fill="currentColor"></path></svg><svg style="display:none;" class="icon" focusable="false" viewBox="0 0 32 32"><path d="M13.502 5.414a15.075 15.075 0 0 0 11.594 18.194a11.113 11.113 0 0 1-7.975 3.39c-.138 0-.278.005-.418 0a11.094 11.094 0 0 1-3.2-21.584M14.98 3a1.002 1.002 0 0 0-.175.016a13.096 13.096 0 0 0 1.825 25.981c.164.006.328 0 .49 0a13.072 13.072 0 0 0 10.703-5.555a1.01 1.01 0 0 0-.783-1.565A13.08 13.08 0 0 1 15.89 4.38A1.015 1.015 0 0 0 14.98 3z" fill="currentColor"></path></svg></button><form class="search-box" role="search"><input type="search" placeholder="Search" autocomplete="off" spellcheck="false" value><!----></form></div></header><!--]--><div class="sidebar-mask"></div><!--[--><aside class="sidebar"><nav class="navbar-items"><!--[--><div class="navbar-item"><a href="/webpack5-doc/base/" class="router-link-active" aria-label="基础"><!--[--><!--]--> 基础 <!--[--><!--]--></a></div><div class="navbar-item"><a href="/webpack5-doc/progress/" class="" aria-label="进阶"><!--[--><!--]--> 进阶 <!--[--><!--]--></a></div><div class="navbar-item"><a href="/webpack5-doc/senior/" class="" aria-label="高级"><!--[--><!--]--> 高级 <!--[--><!--]--></a></div><div class="navbar-item"><a href="/webpack5-doc/project/" class="" aria-label="项目"><!--[--><!--]--> 项目 <!--[--><!--]--></a></div><div class="navbar-item"><a href="/webpack5-doc/origin/" class="" aria-label="原理"><!--[--><!--]--> 原理 <!--[--><!--]--></a></div><!--]--></nav><!--[--><!--]--><ul class="sidebar-items"><!--[--><li><p tabindex="0" class="sidebar-item sidebar-heading">基础配置 <!----></p><!--[--><ul style="" class="sidebar-item-children"><!--[--><li><a href="/webpack5-doc/base/" class="router-link-active sidebar-item" aria-label="基本配置"><!--[--><!--]--> 基本配置 <!--[--><!--]--></a><!----></li><li><a href="/webpack5-doc/base/development.html" class="sidebar-item" aria-label="开发模式介绍"><!--[--><!--]--> 开发模式介绍 <!--[--><!--]--></a><!----></li><li><a href="/webpack5-doc/base/css.html" class="sidebar-item" aria-label="处理样式资源"><!--[--><!--]--> 处理样式资源 <!--[--><!--]--></a><!----></li><li><a href="/webpack5-doc/base/image.html" class="sidebar-item" aria-label="处理图片资源"><!--[--><!--]--> 处理图片资源 <!--[--><!--]--></a><!----></li><li><a href="/webpack5-doc/base/output.html" class="sidebar-item" aria-label="修改输出资源的名称和路径"><!--[--><!--]--> 修改输出资源的名称和路径 <!--[--><!--]--></a><!----></li><li><a href="/webpack5-doc/base/clean.html" class="sidebar-item" aria-label="自动清空上次打包资源"><!--[--><!--]--> 自动清空上次打包资源 <!--[--><!--]--></a><!----></li><li><a href="/webpack5-doc/base/font.html" class="sidebar-item" aria-label="处理字体图标资源"><!--[--><!--]--> 处理字体图标资源 <!--[--><!--]--></a><!----></li><li><a href="/webpack5-doc/base/other.html" class="sidebar-item" aria-label="处理其他资源"><!--[--><!--]--> 处理其他资源 <!--[--><!--]--></a><!----></li><li><a href="/webpack5-doc/base/javascript.html" class="sidebar-item" aria-label="处理 js 资源"><!--[--><!--]--> 处理 js 资源 <!--[--><!--]--></a><!----></li><li><a href="/webpack5-doc/base/html.html" class="sidebar-item" aria-label="处理 Html 资源"><!--[--><!--]--> 处理 Html 资源 <!--[--><!--]--></a><!----></li><li><a href="/webpack5-doc/base/server.html" class="sidebar-item" aria-label="开发服务器&amp;自动化"><!--[--><!--]--> 开发服务器&amp;自动化 <!--[--><!--]--></a><!----></li><li><a href="/webpack5-doc/base/production.html" class="sidebar-item" aria-label="生产模式介绍"><!--[--><!--]--> 生产模式介绍 <!--[--><!--]--></a><!----></li><li><a href="/webpack5-doc/base/optimizeCss.html" class="sidebar-item" aria-label="Css 处理"><!--[--><!--]--> Css 处理 <!--[--><!--]--></a><!----></li><li><a href="/webpack5-doc/base/minifyHtml.html" class="sidebar-item" aria-label="html 压缩"><!--[--><!--]--> html 压缩 <!--[--><!--]--></a><!----></li><li><a href="/webpack5-doc/base/summary.html" class="sidebar-item" aria-label="总结"><!--[--><!--]--> 总结 <!--[--><!--]--></a><!----></li><!--]--></ul><!--]--></li><!--]--></ul><!--[--><!--]--></aside><!--]--><!--[--><main class="page"><!--[--><!--]--><div class="theme-default-content"><!--[--><h1 id="基本使用" tabindex="-1"><a class="header-anchor" href="#基本使用" aria-hidden="true">#</a> 基本使用</h1><p><strong><code>Webpack</code> 是一个静态资源打包工具。</strong></p><p>它会以一个或多个文件作为打包的入口，将我们整个项目所有文件编译组合成一个或多个文件输出出去。</p><p>输出的文件就是编译好的文件，就可以在浏览器段运行了。</p><p>我们将 <code>Webpack</code> 输出的文件叫做 <code>bundle</code>。</p><h2 id="功能介绍" tabindex="-1"><a class="header-anchor" href="#功能介绍" aria-hidden="true">#</a> 功能介绍</h2><p>Webpack 本身功能是有限的:</p><ul><li>开发模式：仅能编译 JS 中的 <code>ES Module</code> 语法</li><li>生产模式：能编译 JS 中的 <code>ES Module</code> 语法，还能压缩 JS 代码</li></ul><h2 id="开始使用" tabindex="-1"><a class="header-anchor" href="#开始使用" aria-hidden="true">#</a> 开始使用</h2><h3 id="_1-资源目录" tabindex="-1"><a class="header-anchor" href="#_1-资源目录" aria-hidden="true">#</a> 1. 资源目录</h3><div class="language-text ext-text line-numbers-mode"><pre class="language-text"><code>webpack_code # 项目根目录（所有指令必须在这个目录运行）
    └── src # 项目源码目录
        ├── js # js文件目录
        │   ├── count.js
        │   └── sum.js
        └── main.js # 项目主文件
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><h3 id="_2-创建文件" tabindex="-1"><a class="header-anchor" href="#_2-创建文件" aria-hidden="true">#</a> 2. 创建文件</h3><ul><li>count.js</li></ul><div class="language-javascript ext-js line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token keyword">function</span> <span class="token function">count</span><span class="token punctuation">(</span><span class="token parameter">x<span class="token punctuation">,</span> y</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> x <span class="token operator">-</span> y<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><ul><li>sum.js</li></ul><div class="language-javascript ext-js line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token keyword">function</span> <span class="token function">sum</span><span class="token punctuation">(</span><span class="token parameter"><span class="token operator">...</span>args</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> args<span class="token punctuation">.</span><span class="token function">reduce</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">p<span class="token punctuation">,</span> c</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> p <span class="token operator">+</span> c<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><ul><li>main.js</li></ul><div class="language-javascript ext-js line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">import</span> count <span class="token keyword">from</span> <span class="token string">&quot;./js/count&quot;</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> sum <span class="token keyword">from</span> <span class="token string">&quot;./js/sum&quot;</span><span class="token punctuation">;</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">count</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">sum</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h3 id="_3-下载依赖" tabindex="-1"><a class="header-anchor" href="#_3-下载依赖" aria-hidden="true">#</a> 3. 下载依赖</h3><p>打开终端，来到项目根目录。运行以下指令：</p><ul><li>初始化<code>package.json</code></li></ul><div class="language-text ext-text line-numbers-mode"><pre class="language-text"><code>npm init -y
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br></div></div><p>此时会生成一个基础的 <code>package.json</code> 文件。</p><p><strong>需要注意的是 <code>package.json</code> 中 <code>name</code> 字段不能叫做 <code>webpack</code>, 否则下一步会报错</strong></p><ul><li>下载依赖</li></ul><div class="language-text ext-text line-numbers-mode"><pre class="language-text"><code>npm i webpack webpack-cli -D
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br></div></div><h3 id="_4-启用-webpack" tabindex="-1"><a class="header-anchor" href="#_4-启用-webpack" aria-hidden="true">#</a> 4. 启用 Webpack</h3><ul><li>开发模式</li></ul><div class="language-text ext-text line-numbers-mode"><pre class="language-text"><code>npx webpack ./src/main.js --mode=development
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br></div></div><ul><li>生产模式</li></ul><div class="language-text ext-text line-numbers-mode"><pre class="language-text"><code>npx webpack ./src/main.js --mode=production
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br></div></div><p><code>npx webpack</code>: 是用来运行本地安装 <code>Webpack</code> 包的。</p><p><code>./src/main.js</code>: 指定 <code>Webpack</code> 从 <code>main.js</code> 文件开始打包，不但会打包 <code>main.js</code>，还会将其依赖也一起打包进来。</p><p><code>--mode=xxx</code>：指定模式（环境）。</p><h3 id="_5-观察输出文件" tabindex="-1"><a class="header-anchor" href="#_5-观察输出文件" aria-hidden="true">#</a> 5. 观察输出文件</h3><p>默认 <code>Webpack</code> 会将文件打包输出到 <code>dist</code> 目录下，我们查看 <code>dist</code> 目录下文件情况就好了</p><h2 id="小结" tabindex="-1"><a class="header-anchor" href="#小结" aria-hidden="true">#</a> 小结</h2><p><code>Webpack</code> 本身功能比较少，只能处理 <code>js</code> 资源，一旦遇到 <code>css</code> 等其他资源就会报错。</p><p>所以我们学习 <code>Webpack</code>，就是主要学习如何处理其他资源。</p><!--]--></div><footer class="page-meta"><!----><!----><!----></footer><!----><!--[--><!--]--></main><!--]--></div><!----><!--]--></div>
    <script type="module" src="/webpack5-doc/assets/app.69823687.js" defer></script>
  </body>
</html>
